package video

import (
	"errors"
	"gitee.com/mootok/mootok/src/constant/config"
	"gitee.com/mootok/mootok/src/models"
	"gitee.com/mootok/mootok/src/storage/database"
	"gitee.com/mootok/mootok/src/storage/file"
	"gorm.io/gorm"
	strings2 "strings"
	"time"
)

type Status = int

const (
	StatusAuditing Status = iota
	StatusAuditFailed
	StatusTranscoding
	StatusTranscodeFailed
	StatusPublished
)

type Service struct{}

var LinkExpirationTime = int64((48 * time.Hour).Seconds())

// IsPublished checks whether video has published
func (*Service) IsPublished(videoId int) (bool, error) {
	if err := database.Client.First(&models.Video{
		ID: videoId,
		Status: StatusPublished,
	}).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return false, nil
		}
		return false, err
	}
	return true, nil
}

func (*Service) GetVideoCover(cover string) string {
	return file.Instance.GetLink(cover, LinkExpirationTime, nil)
}

func (s *Service) GetTranscodeUrl(fileName string, policy config.TranscodePolicy) string {
	return fileName + "-adapt/" + strings2.ToLower(policy.Name) + ".m3u8"
}
